#include "PPMImage.h"
#include "stdafx.h"

PPMImage::PPMImage(){}

PPMImage::PPMImage(char* filename1)
{
	filename = filename1;
	width = 0;
	height = 0;
	maxValue = 0;
}

PPMImage::~PPMImage()
{
}

PPMImage& PPMImage::operator=(const PPMImage& image)
{
	filename = image.filename;
	width = image.width;
	height = image.height;
	maxValue = image.maxValue;
	pix = image.pix;
	return *this;
}

int PPMImage::getWidth()
{
	return width;
}

int PPMImage::getHeight()
{
	return height;
}

int PPMImage::getMaxValue()
{
	return maxValue;
}

unsigned char PPMImage::getPix(int x, int y)
{
	return *(pix+x+y*width*3);
}

Couleur PPMImage::getCouleur(int x, int y)
{
	x = x*3;
	float rouge =  getPix(x,y)*1.0f/getMaxValue();
	float vert =  getPix(x+1,y)*1.0f/getMaxValue();
	float bleu =  getPix(x+2,y)*1.0f/getMaxValue();

	return Couleur(rouge, vert, bleu);
}

vector<string> PPMImage::split(string src, string separator)
{
	vector<string> dest;
    string str = src;
    string substring;
    string::size_type start = 0, index;

    do
    {
        index = str.find_first_of(separator,start);
        if (index != string::npos)
        {   
            substring = str.substr(start,index-start);
            dest.push_back(substring);
            start = str.find_first_not_of(separator,index);
            if (start == string::npos) 
				break;
        }
    }while(index != string::npos);
   
    //the last token
	if (start != string::npos) 
    {
		substring = str.substr(start);
		dest.push_back(substring);
	}

	return dest;
}

void PPMImage::traiteImage()
{
	std::ifstream fichier(filename, std::ios::in);  // on ouvre en lecture

	if(fichier)  // si l'ouverture a fonctionn?
	{
		string contenu; 
		vector<string> strList;

		while(width == 0 || height == 0 || maxValue ==0)
		{
			getline(fichier,contenu); // width, height
		
			strList= split(contenu, " ");

			for(size_t i=0 ; i < strList.size() ; i ++)
			{
				string strTemp = strList.at(i);
				if(strTemp.at(0) == '#') //commentaires
					break;
				if(width == 0)
				{
					std::istringstream  istr(strTemp);
					istr >> width;
				}
				else if(height == 0)
				{
					std::istringstream  istr(strTemp);
					istr >> height;
				}
				else
				{
					std::istringstream  istr(strTemp);
					istr >> maxValue;
					break;
				}
			}
		}

		pix = new unsigned char[width*height*3];
		int index = 0;

		while(getline(fichier,contenu))
		{
			strList= split(contenu, " ");
			if(strList.at(0).at(0) == '#') //commentaires
					continue;

			for(size_t i = 0; i< strList.size() ; i ++)
			{
				std::istringstream  istr(strList.at(i));
				int temp;
				istr >> temp;
				pix[index] = (unsigned char)temp;
				index++;
			}

		}
		 
		fichier.close();
	}
	else
		std::cerr << "Impossible d'ouvrir le fichier !" << std::endl;

}

void PPMImage::save()
{
	FILE * pFile;
	
	fopen_s(&pFile, "1.ppm","w");
	fprintf (pFile, "P3\r%d %d\r255\r", width, height);
	
	for (int i = 0; i < width*height*3 ; i+=3)
	{
		fprintf (pFile, "%d %d %d ", pix[i], pix[i+1], pix[i+2]);
	}
	fclose(pFile);
}